package stu.day1_day8;

import java.util.ArrayDeque;
import java.util.Iterator;

public class LeetCode71 {
    public String simplifyPath(String path) {
        ArrayDeque<String> stack = new ArrayDeque<String>();
        String[] p = path.split("/+");

        for (String i : p) {
            if (i != "") {
                if (i.equals(".")) {
                    continue;
                } else if (i.equals("..")) {
                    if(!stack.isEmpty()){
                        stack.pop();
                    }

                } else {
                    stack.push(i);
                }
            }
        }

        String str="";

        if (stack.isEmpty()) {
            return "/";
        } else {
            while (!stack.isEmpty()) {
                str = "/"+stack.pop()+str;
            }
            return str;
        }

    }

    public static void main(String[] args) {
        ArrayDeque<String> stack = new ArrayDeque<String>();
        stack.addLast("xy");
        stack.addLast("1");
        stack.addLast("--");
        System.out.println(stack.removeLast());
        Iterator<String> path = stack.iterator();
        while (path.hasNext()){
            System.out.println(path.next());
        }


    }
}
